package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.*;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.service.LogisticsSignbillService;
import com.yidu.logisticssystem.service.LogisticsWaybillService;
import com.yidu.logisticssystem.service.LogisticsWaytypeService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户签单表(LogisticsSignbill)表控制层
 *
 * @author makejava
 * @since 2021-03-08 16:46:54
 */
@Api(tags= "用户签单接口")
@RestController
@RequestMapping("logisticsSignbill")
public class LogisticsSignbillController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsSignbillService logisticsSignbillService;
    @Resource
    private LogisticsMapService mapService;


    /***
     * 条件查询
     * @param logisticsSignbill
     * @return 所有数据
     */
    @ApiOperation(value = "条件查询用户签单信息",notes = "返回用户签单信息")
    @GetMapping("queryAll")
    public Map<String, Object> findAll(LogisticsSignbill logisticsSignbill,int page,int limit,HttpServletRequest request){
        //将参数传入分页助手
        PageHelper.startPage(page,limit);
        List<LogisticsSignbill> list=logisticsSignbillService.queryAll(logisticsSignbill,request);
        List<LogisticsSignbill> lists=new ArrayList<>();
        for(LogisticsSignbill bill:list){
            //把发货地点的id分割出来查询
            LogisticsMap map=mapService.queryById(bill.getSbSendarea().split(",")[0]);
            //把收货地点的id分割出来查询
            LogisticsMap tomap=mapService.queryById(bill.getSbConsignarea().split(",")[0]);
            //设置发货地点
            bill.setSbSendarea(map.getMapName()+bill.getSbSendarea().split(",")[1]);
            //设置收货地点
            bill.setSbConsignarea(tomap.getMapName()+bill.getSbConsignarea().split(",")[1]);
            lists.add(bill);
        }
        Map<String, Object>map=new HashMap<>();
        //查询到集合之后，将集合封装到分页助手的PageInfo对象中
        PageInfo<LogisticsSignbill> pageInfo=new PageInfo<>(list);
        map.put("code",0);
        map.put("mes","");
        map.put("data",lists);
        map.put("count",pageInfo.getTotal());//总行数自动生成
        return map;
    }
    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据查询用户签单信息",notes = "返回单条数据")
    @GetMapping("selectOne")
    public BaseMessage selectOne(Integer id) {
        //根据id查找用户签单信息
        LogisticsSignbill bill=logisticsSignbillService.queryById(id);
        //将收货地点用逗号分割出来
        String []area=bill.getSbConsignarea().split(",");
        //将发货地点用逗号分割出来
        String []to=bill.getSbSendarea().split(",");
        //将收货地点id放入SbConsignarea_
        bill.setSbConsignarea_(Integer.valueOf(area[0]));
        //将收货详细地点放入SbConsignarea_1
        bill.setSbConsignarea_1(area[1]);
        //将发货地点id放入SbSendarea_
        bill.setSbSendarea_(Integer.valueOf(to[0]));
        //将发货详细地点放入SbSendarea_1
        bill.setSbSendarea_1(to[1]);
        return BaseMessage.succes(bill);
    }

    /**
     * 增加或者修改
     * @param logisticsSignbill
     * @return 改变的行数
     */
    @ApiOperation(value = "增加或修改用户签单信息",notes = "返回改变的行数")
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsSignbill logisticsSignbill, HttpServletRequest servletRequest){
        int rows=0;
        if(logisticsSignbill.getSignbillId()==null){
            rows=logisticsSignbillService.insert(logisticsSignbill,servletRequest);
        }else {
            rows=logisticsSignbillService.update(logisticsSignbill);

        }
        return rows>0?BaseMessage.succes():BaseMessage.fail();
    }

    /**
     * 删除用户签单信息
     * @return 改变的行数
     */
    @ApiOperation(value = "删除用户签单信息",notes = "返回改变的行数")
    @PostMapping("delete")
    public BaseMessage delete(LogisticsSignbill logisticsSignbill){
        boolean b=false;
        b=logisticsSignbillService.deleteById(logisticsSignbill.getSignbillId());
        return b==true?BaseMessage.succes():BaseMessage.fail();
    }

    /**
     * 条件查询已接的订单
     * @param logisticsSignbill
     * @return
     */
    @ApiOperation(value = "条件查询已接的订单",notes = "返回用户签单信息")
    @GetMapping("queryway")
    public BaseMessage queryway(LogisticsSignbill logisticsSignbill){
        //条件查询已接的订单
        List<LogisticsSignbill>list=logisticsSignbillService.queryway(logisticsSignbill);
        if(list.isEmpty()){
            return BaseMessage.fail();
        }else {
            return BaseMessage.succes();
        }
    }

    /***
     * 条件查询未接的订单
     * @param logisticsSignbill
     * @return 所有数据
     */
    @ApiOperation(value = "条件查询未接的订单",notes = "返回用户签单信息")
    @GetMapping("query")
    public Map<String, Object> find(LogisticsSignbill logisticsSignbill,int page,int limit){
        //将参数传入分页助手
        PageHelper.startPage(page,limit);
        List<LogisticsSignbill> list=logisticsSignbillService.query(logisticsSignbill);
        Map<String, Object>map=new HashMap<>();
        //查询到集合之后，将集合封装到分页助手的PageInfo对象中
        PageInfo<LogisticsSignbill> pageInfo=new PageInfo<>(list);
        map.put("code",0);
        map.put("mes","");
        map.put("data",list);
        map.put("count",pageInfo.getTotal());//总行数自动生成
        return map;
    }

    /**
     *  根据货运单编号查询地区
     * @param waybill
     * @return
     */
    @ApiOperation(value = "根据货运单编号查询地区",notes = "返回地区信息")
    @PostMapping("findWayNum")
    public BaseMessage findWayNum(LogisticsSignbill waybill){
        LogisticsSignbill bill=logisticsSignbillService.queryWaynum(waybill.getWayNum());
        //将发货地址用逗号分割出来
        String []tomap=bill.getSbSendarea().split(",");
        //收货地址
        String []gomap=bill.getSbConsignarea().split(",");
        //根据发货地区id查询经纬度
        LogisticsMap map=mapService.queryById(tomap[0]);
        //根据收货地区id查询经纬度
        LogisticsMap maps=mapService.queryById(gomap[0]);
        //将经度设入map对象
        map.setTomapEast(maps.getMapEast());
        //将维度设入map对象
        map.setTomapNorth(maps.getMapNorth());
        return BaseMessage.succes(map);
    }
}
